package controller

import (
	"gin/response"
	"gin/service"
	"gin/types"
	"github.com/gin-gonic/gin"
)

var (
	WorkBenchService service.Workbench
	TaskAbnormalService     service.TaskAbnormal
)

type Workbench struct {
}

// GlobalAbnormalCount 资产统计列表
func (w *Workbench) GlobalAbnormalCount(ctx *gin.Context) {

	var req types.GlobalAbnormalCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.GlobalAbnormalCount(userInfo, req)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// PersonalTaskCount 个人任务统计列表
func (w *Workbench) PersonalTaskCount(ctx *gin.Context) {

	var req types.PersonalTaskCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.PersonalTaskCount(userInfo, req)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// StepCategoryTaskCount 环节任务统计列表
func (w *Workbench) StepCategoryTaskCount(ctx *gin.Context) {

	var req types.StepCategoryTaskCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.StepCategoryTaskCount(ctx, req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// GroupUpstreamDeliveryCount 部门任务统计列表
func (w *Workbench) GroupUpstreamDeliveryCount(ctx *gin.Context) {

	var req types.GroupTaskCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.GroupUpstreamDeliveryCount(ctx, req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// GroupPlanDoneCount 部门任务统计列表
func (w *Workbench) GroupPlanDoneCount(ctx *gin.Context) {

	var req types.GroupTaskCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.GroupPlanDoneCount(req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// GroupDeliveryCount 部门任务统计列表
func (w *Workbench) GroupDeliveryCount(ctx *gin.Context) {

	var req types.GroupTaskCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.GroupDeliveryCount(req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// GroupAbnormalAddCount 部门任务统计列表
func (w *Workbench) GroupAbnormalAddCount(ctx *gin.Context) {

	var req types.GroupTaskCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.GroupAbnormalAddCount(req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// GroupAbnormalWorkOrderCount 部门任务统计列表
func (w *Workbench) GroupAbnormalWorkOrderCount(ctx *gin.Context) {

	var req types.GroupTaskCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.GroupAbnormalWorkOrderCount(req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// GroupAbnormalDelayTaskCount 部门任务统计列表
func (w *Workbench) GroupAbnormalDelayTaskCount(ctx *gin.Context) {

	var req types.GroupTaskCountReq
	err := ctx.ShouldBindJSON(&req)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.GroupAbnormalDelayTaskCount(req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// EpisodeDetail 剧集封板详情
func (w *Workbench) EpisodeDetail(ctx *gin.Context) {
	var req types.EpisodeDetailReq
	err := ctx.ShouldBindJSON(&req)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.EpisodeDetail(req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}

}

// StepCategoryInprogressEpisodeList 环节进行中剧集列表
func (w *Workbench) StepCategoryInprogressEpisodeList(ctx *gin.Context) {
	var req types.StepCategoryInprogressEpisodeListReq
	err := ctx.ShouldBindJSON(&req)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.StepCategoryInprogressEpisodeList(ctx, req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// StepCategoryReviewingEpisodeList 环节审核中的剧集情况
func (w *Workbench) StepCategoryReviewingEpisodeList(ctx *gin.Context) {
	var req types.StepCategoryInprogressEpisodeListReq
	err := ctx.ShouldBindJSON(&req)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.StepCategoryReviewingEpisodeList(ctx, req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// StepCategorySealEpisodeList 环节封板剧集情况查询
func (w *Workbench) StepCategorySealEpisodeList(ctx *gin.Context) {
	var req types.StepCategoryInprogressEpisodeListReq
	err := ctx.ShouldBindJSON(&req)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	userInfo := service.GetUserInfoFromContext(ctx, "userInfo")
	resData, queryErr := WorkBenchService.StepCategorySealEpisodeList(ctx, req, userInfo)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// EpisodeSealShowList 剧集封板播出情况列表
func (w *Workbench) EpisodeSealShowList(ctx *gin.Context) {
	var req types.EpisodeSealShowListReq
	err := ctx.ShouldBindJSON(&req)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	resData, queryErr := WorkBenchService.EpisodeSealShowList(req)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// MakingOverview 制作情况概览
func (w *Workbench) MakingOverview(ctx *gin.Context) {
	var req types.MakingOverviewReq
	err := ctx.ShouldBindJSON(&req)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	resData, queryErr := WorkBenchService.MakingOverview(ctx, req)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

//AbnormalTaskCount 今日异常任务统计
func (w *Workbench) ToDayAbnormalTaskCount(ctx *gin.Context) {
	var req types.AbnormalCountReq
	err := ctx.ShouldBindJSON(&req)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	tenantId := service.GetInt64FromContext(ctx, "tenant_id")
	resData, queryErr := TaskAbnormalService.Count(req, tenantId)
	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}
